package com.five.community.controller;


import com.five.community.bean.Activity;
import com.five.community.bean.Village;
import com.five.community.bean.vo.ActivityVo;

import com.five.community.bean.vo.RepairVo;
import com.five.community.contant.AppContants;
import com.five.community.service.ActivityService;
import com.five.community.service.VillageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private VillageService villageService;
    //活动一览页面
    @RequestMapping("/listUi")
    public String listUi(ModelMap map,Integer pageNum,String title){
        if (pageNum == null) {
            pageNum = 1;
        }
        if(title==null||title=="") {
            try {
                PageInfo<ActivityVo> pageInfo = activityService.getPageInfo(pageNum, AppContants.pageSize);
                map.addAttribute("pageInfo", pageInfo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        else {try {

            PageHelper.startPage(pageNum,AppContants.pageSize);
            List<ActivityVo> activityVos = activityService.selectByTitle(title);
            System.out.println(activityVos);
            PageInfo<ActivityVo> pageInfo = new PageInfo<>(activityVos);
            map.addAttribute("pageInfo", pageInfo);
            Activity activity = new Activity();
            activity.setTitle(title);
            map.addAttribute("activity",activity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
        return "activitylist";
    }
    //通过活动标题搜索
    @RequestMapping("/selectUi")
    public String selectUi(ModelMap map,String title,Integer pageNum){

        if (title=="") {
            return "redirect:/activity/listUi";
        } else {try {
            if (pageNum == null) {
                pageNum = 1;
            }
            Activity activity = new Activity();
            activity.setTitle(title);
            map.addAttribute("activity",activity);
            PageHelper.startPage(pageNum,AppContants.pageSize);
            List<ActivityVo> activityVos = activityService.selectByTitle(title);
            PageInfo<ActivityVo> pageInfo = new PageInfo<>(activityVos);
            map.addAttribute("pageInfo", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
        return "activitylist";
    }
    //活动信息删除
    @RequestMapping("/limit/delete")
    public String delete(long id){
        try {
            activityService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/activity/listUi";
    }
    //添加页面
    @RequestMapping("/limit/addUi")
    public String addUi(ModelMap map) {
        try {
            List<Village> villages = villageService.selectAll();
            map.addAttribute("villages", villages);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "activity_add";
    }
    //添加活动信息
    @RequestMapping("/add")
    public String add(ActivityVo activityVo){
        try {
            long vid = Long.parseLong(activityVo.getVname());
            Activity activity = new Activity(vid,activityVo.getTitle(),activityVo.getPlace(),activityVo.getOrganizer(),activityVo.getStartDate(),activityVo.getEndDate(),new Date(), activityVo.isState());
            activityService.insert(activity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/activity/addUi";
    }
    //修改编辑页面
    @RequestMapping("/limit/editUi")
    public String editUi(long id, ModelMap map){
        try {
            Activity activity = activityService.selectById(id);
            map.addAttribute("activity",activity);
            List<Village> villages = villageService.selectAll();
            map.addAttribute("villages",villages);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "activity_edit";
    }
    //修改编辑活动
    @RequestMapping("/edit")
    public String edit(ActivityVo activityVo){
        try {
            long vid = Long.parseLong(activityVo.getVname());
            Activity activity = new Activity(activityVo.getId(), vid,activityVo.getTitle(),activityVo.getPlace(),activityVo.getOrganizer(),activityVo.getStartDate(),activityVo.getEndDate(),activityVo.getCreateDate(), activityVo.isState());
            activityService.update(activity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "activity_edit";
    }

}
